package com.xiaoniu.lib_keleim.pojo.vos;

import com.xiaoniu.lib_keleim.ConversationType;
import com.xiaoniu.lib_keleim.Schemes;
import com.xiaoniu.lib_keleim.db.manager.MessageDao;
import com.xiaoniu.lib_keleim.pojo.dos.MessageEntity;
import com.xiaoniu.lib_keleim.pojo.dos.UserEntity;

public class BaseMessage extends BaseBean {
    public long messageId;
    //目标
    public long targetId;
    //发送人用户ID
    public long userId;
    public UserEntity sendUser;
    //会话类型 聊天室:0 私聊 :1
    public byte conversationType;
    //消息转发协议
    public Schemes uriScheme;
    //消息类型
    public int msgType;
    //消息发生时间
    public long timestamp;
    //消息内容
    public String content;
    //额外的信息
    public Object ext;
    //0 	客户端存储	支持离线消息?机制，且存入服务端历史消息	计入未读消息数
    //1 	支持离线消息?机制	不计入未读消息数
    //2 	客户端存储	支持离线消息?机制，且存入服务端历史消息	不计入未读消息数
    //3 	客户端不存储	服务端不存储	不计入未读消息数
    //4 	客户端存储	服务端不存储	不计入未读消息数
    public int flag;

    //0 server 1 android 2 ios 3 web 4 pc
    public byte platformIdentity = 1;

    public long getTargetUserId(){
        if (userId == UserInfoVO.getSingleton().getCustomerId()) {
            return targetId;
        }else{
            return userId;
        }
    }

    public UserEntity getTargetUserInfo(){
        long targetUserId = getTargetUserId();
        return MessageDao.getInstance().queryTargetUserEntity(targetUserId);
    }

    public MessageEntity toMessageEntity(){
        MessageEntity messageEntity = new MessageEntity();
        messageEntity.messageId = messageId;
        messageEntity.targetId = targetId;
        messageEntity.userId = userId;
        messageEntity.conversationType = conversationType;
        messageEntity.msgType = (byte) msgType;
        messageEntity.timestamp = timestamp;
        messageEntity.content = content;
        messageEntity.platformIdentity = platformIdentity;
        messageEntity.setExt(ext);
        return messageEntity;
    }

    public BaseMessage toMessageChatVO(){
        BaseMessage messageChatVO = new BaseMessage();
        messageChatVO.messageId = messageId;
        messageChatVO.targetId = targetId;
        messageChatVO.userId = userId;
        messageChatVO.conversationType = conversationType;
        messageChatVO.msgType = msgType;
        messageChatVO.timestamp = timestamp;
        messageChatVO.content = content;
        messageChatVO.platformIdentity = platformIdentity;
        messageChatVO.ext = ext;
        messageChatVO.sendUser = sendUser;
        return messageChatVO;
    }

    public MessageConversationVO toConversationVO(){
        return toConversationVO(0);
    }

    public MessageConversationVO toConversationVO(long fromUser){

        BaseMessage messageChatVO = new BaseMessage();
        messageChatVO.messageId = messageId;
        messageChatVO.targetId = targetId;
        messageChatVO.userId = userId;
        messageChatVO.conversationType = conversationType;
        messageChatVO.msgType = msgType;
        messageChatVO.timestamp = timestamp;
        messageChatVO.content = content;
        messageChatVO.platformIdentity = platformIdentity;
        if (fromUser==0) {
            messageChatVO.sendUser = MessageDao.getInstance().queryTargetUserEntity(userId);
        }

        MessageConversationVO conversationVO = new MessageConversationVO();
        conversationVO.lastMessageId = messageId;
        conversationVO.targetId = targetId;
        conversationVO.userId = userId;
        conversationVO.updateTime = timestamp;
        conversationVO.createTime = timestamp;
        conversationVO.messageChatVO = messageChatVO;
        return conversationVO;
    }

    public static BaseMessage obtain(String targetId, ConversationType conversationType, String content){
        return obtain(null,targetId,conversationType,"",content);
    }

    public static BaseMessage obtain(Schemes uriScheme,String targetId, ConversationType conversationType, String channelId, String content){
        BaseMessage message = new BaseMessage();
        message.targetId = Long.parseLong(targetId);
        if (uriScheme==null) {
            message.uriScheme = Schemes.RONGYUN;
        }else{
            message.uriScheme = uriScheme;
        }
        message.conversationType = (byte) conversationType.getValue();
        message.content = content;
        return message;
    }
}
